#!/bin/bash

#--------------------------------------------------------------------------------
# Example demonstrating use of morph scripts and file formats
# Author: Marelie Davel (marelie.davel@gmail.com)
#--------------------------------------------------------------------------------

root_dir=~/gcode/cx-morph-kit
src_dir=$root_dir/src
input_dir=$root_dir/examples/afr_placenames/input
data_dir=$root_dir/examples/afr_placenames/results
name=example.afr_placenames

#--------------------------------------------------------------------------------

# Reformat characters and extract symbols
# input:   <xxx.pairs> (raw 2-column data)
# output:  <xxx.sym>
# command: perl $src_dir/extract_symset/extract_symset.pl <$input_dir/xxx.pairs> <$data_dir/xxx.sym>

perl $src_dir/extract_symset/extract_symset.pl $input_dir/$name.pairs $data_dir/$name.sym

#--------------------------------------------------------------------------------

# Screen data (according to options in the .spec file)
# input:   <xxx.sym>, <xxx.spec> (option specification file)
# output:  <xxx.sym.screened>
# command: perl $src_dir/morph_rule/screen.pl <$data_dir/xxx.sym> <$input_dir/xxx.spec> <$data_dir/xxx.sym.screened>

perl $src_dir/morph_rule/screen.pl $data_dir/$name.sym $input_dir/$name.spec $data_dir/$name.sym.screened

#--------------------------------------------------------------------------------

# Reformat for D&R rule extraction
# input:   <xxx.sym.screened>
# output:  <xxx.defref-pat>, <xxx.defref-class>
# command: perl $src_dir/morph_rule/reformat.pl <$data_dir/xxx.sym.screened> <$data_dir/xxx.defref-pat> <$data_dir/xxx.defref-class>

perl $src_dir/morph_rule/reformat.pl $data_dir/$name.sym.screened $data_dir/$name.defref-pat $data_dir/$name.defref-class 

#--------------------------------------------------------------------------------

# Extract D&R rules
# input: <xxx.defref-pat> <xxx.defref-class>
# output: <xxx.defref-rules> <xxx.defref-feats> <xxx.defref-log>
# command: $src_dir/morph_rule/extract_rules.sh <$data_dir/xxx.defref-pat> <$data_dir/xxx.defref-class> <$data_dir/xxx.defref-rules> <$data_dir/xxx.defref-gnulls> <$data_dir/xxx.defref-feats> <$data_dir/xxx.defref-log>

cd $src_dir/morph_rule
$src_dir/morph_rule/extract_rules.sh $data_dir/$name.defref-pat $data_dir/$name.defref-class $data_dir/$name.defref-rules $data_dir/$name.defref-gnulls $data_dir/$name.defref-feats $data_dir/$name.defref-log

#--------------------------------------------------------------------------------

# Analyse rules
# input: <xxx.defref-rules>, <xxx.defref-class>, <xxx.sym.screened>
# output: <xxx.defref-rules.info>
# command: perl $src_dir/morph_rule/analyse.pl rules <$data_dir/xxx.defref-rules> <$data_dir/xxx.defref-class> <$data_dir/xxx.sym.screened> <$data_dir/xxx.defref-rules.info>


perl $src_dir/morph_rule/analyse.pl rules $data_dir/$name.defref-rules $data_dir/$name.defref-class $data_dir/$name.sym.screened $data_dir/$name.defref-rules.info
echo "#--------------------------------------------------------------------------------"
echo "Example: rule analysis"
echo "#--------------------------------------------------------------------------------"
echo "Analysis in: $data_dir/$name.defref-rules.info"

#--------------------------------------------------------------------------------

# Draw rules
perl $src_dir/morph_rule/draw_rules.pl $data_dir/$name.defref-rules $data_dir/$name.defref-class $data_dir/$name.sym.screened $input_dir/$name.spec $data_dir/$name.draw-rules
echo "#--------------------------------------------------------------------------------"
echo "Example: rule graphs"
echo "#--------------------------------------------------------------------------------"
echo "Rule graph in: $data_dir/$name.draw-rules.*"

#--------------------------------------------------------------------------------

# Predict alternatives for list of words

perl $src_dir/morph_rule/predict.pl alt_list $input_dir/$name.words $data_dir/$name.defref-rules $data_dir/$name.defref-class $data_dir/$name.predicted-alt
echo "#--------------------------------------------------------------------------------"
echo "Example: alternative prediction for word list"
echo "#--------------------------------------------------------------------------------"
echo "Predicted alternatives in: $data_dir/$name.predicted-alt"

#--------------------------------------------------------------------------------

# Predict single best for list of words

perl $src_dir/morph_rule/predict.pl best_list $input_dir/$name.words $data_dir/$name.defref-rules $data_dir/$name.defref-class $data_dir/$name.predicted-1best
echo "#--------------------------------------------------------------------------------"
echo "Example: 1-best prediction for word list"
echo "#--------------------------------------------------------------------------------"
echo "Predicted 1-best in: $data_dir/$name.predicted-1best"

#--------------------------------------------------------------------------------

# Predict alternatives for single word

echo "#--------------------------------------------------------------------------------"
echo "Example: 1-best prediction for single word"
echo "#--------------------------------------------------------------------------------"
perl $src_dir/morph_rule/predict.pl alt gerhardsvlei $data_dir/$name.defref-rules $data_dir/$name.defref-class 

#--------------------------------------------------------------------------------

# Draw categorization network

perl $src_dir/extract_nets/draw_class.pl $data_dir/$name.defref-class $data_dir/$name.sym.screened $input_dir/$name.spec $data_dir/$name.draw-network >& $data_dir/$name.draw-network.log

echo "#--------------------------------------------------------------------------------"
echo "Example: categorization network graphs"
echo "#--------------------------------------------------------------------------------"
echo "Rule graph in: $data_dir/$name.draw-network.*"

#--------------------------------------------------------------------------------

